home *** CD-ROM | disk | FTP | other *** search
- /* This is the file 'bufio.c' ... by ^z, 870830-0913- ... it includes
- * definitions of some buffer words to accumulate information on its
- * way to/from the disks ... use these to speed up operations and reduce
- * disk head movements, in place of calls to fputc(), fread(), fwrite(),
- * etc. ... try to make them very general so that they will simplify
- * other tasks....
- *
- */
-
- #include <stdio.h>
- #include <unix.h>
- #include <storage.h>
- #include <strings.h>
- #include <ctype.h>
- #include <proto.h>
- #include "qndxr.2.h"
-
- /* set up some buffers here to save on disk head movement and speed up
- * operations ... use my simple ZBUF structure for both input and
- * output operations ... keep everything static, local here to this file
- * for safety's sake ... allocate enough items here for all the buffers
- * that we may need for an NMERGE-way merge operation, taking into account
- * the need for input buffers for all the NMERGE *.k and *.p files plus
- * the output files *.k and *.p as well....
- */
-
- static ZBUF zbuffer[2 + 2 * NMERGE];
-
-
- /* function to create a zbuffer and set its parameters appropriately
- */
-
- void create_zbuffer (n, bufsize, buffile, bufitemsize)
- int n, bufitemsize;
- long bufsize;
- FILE *buffile;
- {
- char *make_buf();
-
- zbuffer[n].zbufbase = make_buf (bufsize);
- zbuffer[n].zbufp = zbuffer[n].zbufbase;
- zbuffer[n].zbufcounter = 0;
- zbuffer[n].zbufsize = bufsize;
- zbuffer[n].zbuffilep = buffile;
- zbuffer[n].zbufitemsize = bufitemsize;
- }
-
-
- /* function to free a zbuffer ...
- */
-
- void free_zbuffer (n)
- int n;
- {
- free (zbuffer[n].zbufbase);
- }
-
-
- /* function to return a pointer to the next item in a chosen input
- * buffer 'n'; it reloads the buffer if necessary ... returns NULL
- * pointer when there's nothing left in the file ...
- */
-
- char *next_input_item (n)
- int n;
- {
- char *result;
-
- if (zbuffer[n].zbufcounter == 0)
- load_zbuffer (n);
-
- zbuffer[n].zbufcounter -= zbuffer[n].zbufitemsize;
- if (zbuffer[n].zbufcounter >= 0)
- {
- result = zbuffer[n].zbufp;
- zbuffer[n].zbufp += zbuffer[n].zbufitemsize;
- return (result);
- }
- else
- return (NULL);
- }
-
-
- /* function to load the nth zbuffer appropriately ... it resets the buffer
- * pointers, etc. ... might as well give the user a chance to interrupt (in
- * the Macintosh version) here, as long as we have to go to the disk anyway....
- */
-
- void load_zbuffer (n)
- int n;
- {
- long nread;
- void exit(), check_interrupt();
-
- #ifdef LIGHTSPEED
- nread = zbuffer[n].zbufsize;
- FSRead (zbuffer[n].zbuffilep->refnum, &nread, zbuffer[n].zbufbase);
- #else
- nread = fread (zbuffer[n].zbufbase, sizeof(char),
- (int)zbuffer[n].zbufsize, zbuffer[n].zbuffilep);
- #endif
-
- zbuffer[n].zbufp = zbuffer[n].zbufbase;
- zbuffer[n].zbufcounter = nread;
-
- if (ferror (zbuffer[n].zbuffilep))
- {
- printf ("\nFatal error in load_zbuffer while reading in data!\n");
- printf ("(n=%d, nread=%ld)\n", n, nread);
- exit (1);
- }
-
- #ifdef LIGHTSPEED
- check_interrupt ();
- #endif
- }
-
-
- /* function to return a pointer to the right place to store the next
- * output item for the nth zbuffer ... when the buffer becomes full,
- * it flushes it to disk, resets pointers, etc.
- */
-
- char *next_output_item (n)
- int n;
- {
- char *result;
- void flush_zbuffer();
-
- if (zbuffer[n].zbufcounter == zbuffer[n].zbufsize)
- flush_zbuffer (n);
-
- result = zbuffer[n].zbufp;
- zbuffer[n].zbufp += zbuffer[n].zbufitemsize;
- zbuffer[n].zbufcounter += zbuffer[n].zbufitemsize;
- return (result);
- }
-
-
- /* function to flush out a zbuffer to the disk ... might as well give user
- * a chance to interrupt here (in the Macintosh version), as long as we
- * have to go to the disk anyway....
- */
-
- void flush_zbuffer (n)
- int n;
- {
- long chars_written;
- void exit();
-
- if (zbuffer[n].zbufcounter == 0)
- return;
-
- #ifdef LIGHTSPEED
- chars_written = zbuffer[n].zbufcounter;
- FSWrite (zbuffer[n].zbuffilep->refnum, &chars_written,
- zbuffer[n].zbufbase);
- #else
- chars_written = fwrite (zbuffer[n].zbufbase, sizeof(char),
- (int)zbuffer[n].zbufcounter, zbuffer[n].zbuffilep);
- #endif
- if (ferror(zbuffer[n].zbuffilep) || chars_written == 0)
- {
- printf ("\nFatal error in flush_zbuffer while writing out data!\n");
- printf ("(n=%d, zbufcounter=%ld, chars_written=%ld)\n", n,
- zbuffer[n].zbufcounter, chars_written);
- exit(1);
- }
-
- zbuffer[n].zbufp = zbuffer[n].zbufbase;
- zbuffer[n].zbufcounter = 0;
-
- #ifdef LIGHTSPEED
- check_interrupt ();
- #endif
- }
-
-
-